import type { IInDto, IOutDto } from '#/utils/daprplus';
import { requestClient } from '#/api/request';
import type { ApiResult, PageQuery, PageResult } from '#/utils/daprplus';

export const createCrudApi = <TInDto extends IInDto, TOutDto extends IOutDto>(
  baseUrl: string,
) => {
  return {
    async getPage(
      query: PageQuery,
    ): Promise<{ items: TOutDto[]; total: number }> {
      const result = await requestClient.post<PageResult<TOutDto>>(
        `${baseUrl}/read`,
        query,
        {
          responseReturn: 'data',
        },
      );
      return { items: result.data, total: result.total };
    },

    async create(dtos: TInDto[]): Promise<ApiResult> {
      return await requestClient.post<ApiResult>(baseUrl, { Dtos: dtos });
    },

    async update(dtos: TInDto[]): Promise<ApiResult> {
      return await requestClient.put<ApiResult>(baseUrl, { Dtos: dtos });
    },

    async delete(ids: string[]): Promise<ApiResult> {
      return await requestClient.delete<ApiResult>(baseUrl, ids);
    },
  };
};
